package com.dirven.game.sprite;

import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputProcessor;
import com.dirven.game.DirVenGame;
import com.dirven.game.Grid;
import com.dirven.game.InputManager;
import com.dirven.game.Path;

public class Player extends GridSprite {
	
	int touchDownX,
		touchDownY,
		touchDownPointer;
	
	boolean isDragging = false;
	
	public Player(int x, int y) {
		super(x, y, GridSprite.TYPE_PLAYER, "cycle", "blue.png");
		setDirection(DIRECTION_EAST);
		//System.out.println("new PLAYER()");
		setMovementDisabled(true);
		
		if(Gdx.input.getInputProcessor() == null) {
			System.out.println("NEW INPUT PROCESSOR");
			
			if(DirVenGame.input == null) {
				DirVenGame.input = new InputManager(this);
			}
			
			Gdx.input.setInputProcessor(DirVenGame.input);
		} else {
			DirVenGame.input.setPlayer(this);
		}
	}

	public void determineInput() {
		
		// Keyboard keys controlling direction of movement
		if(Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
		
			setDirection(DIRECTION_WEST);
		}
		if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
			setDirection(DIRECTION_EAST);
		}
		if(Gdx.input.isKeyPressed(Input.Keys.UP)) {
			setDirection(DIRECTION_NORTH);
		}
		if(Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
			setDirection(DIRECTION_SOUTH);
		}		
	}
	
	public void update() {
		// Detect input
		determineInput();
		
		//if(getChainLength() >= 25) {
		//	// Move 1 grid unit
		//	move(false);
		//} else {
			move(true);
		//}
	}

	@Override
	public void movementAI() {
		// Player has no AI.
	}

	@Override
	public void onCollide() {
		onKilled();
	}

	@Override
	public void onKilled() {
	int vibrateTime = 1000; // In MS
		
		// Vibrate on Android when game is over.
		if(DirVenGame.isAndroid()) {
			Gdx.input.vibrate(vibrateTime);
		}
		
			
		DirVenGame.setGameState(DirVenGame.GAMESTATE_GAMEOVER);
	}

}
